package com.xq.tmall.service.impl;

import com.xq.tmall.dao.CategoryMapper;
import com.xq.tmall.entity.Category;
import com.xq.tmall.service.CategoryService;
import com.xq.tmall.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {

    private CategoryMapper categoryMapper;

    @Qualifier("myRedisTemplate")
    @Autowired
    RedisTemplate<Object, List<Object>> myRedisTemplate;


    @Resource(name = "categoryMapper")
    public void setCategoryMapper(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }

    /**
     * @param category
     * @return addStatus
     * @dec 添加分类信息
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public boolean add(Category category) {
        boolean addStatus = categoryMapper.insertOne(category) > 0;
        // 如果缓存存在，删除缓存
        String key = "CATEGORY";
        boolean hasKey = myRedisTemplate.hasKey(key);
        if (hasKey) {
            myRedisTemplate.delete(key);
        }
        return addStatus;
    }

    /**
     * @param category
     * @return updateStatus
     * @dec 修改分类信息
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public boolean update(Category category) {
        boolean updateStatus = categoryMapper.updateOne(category) > 0;
        // 如果缓存存在，删除缓存
        String key = "CATEGORY";
        boolean hasKey = myRedisTemplate.hasKey(key);
        if (hasKey) {
            myRedisTemplate.delete(key);
        }
        return updateStatus;
    }

    /**
     * @param category_name
     * @param pageUtil
     * @return List<Category>
     * @dec 获取分类list(缓存获取)
     */
    @Override
    public List<Category> getList(String category_name, PageUtil pageUtil) {
        String key = "CATEGORY";
        ValueOperations<Object, List<Object>> objectObjectValueOperations = myRedisTemplate.opsForValue();
        // key是否存在
        boolean hasKey = myRedisTemplate.hasKey(key);
        // 缓存是否存在
        if (hasKey) {
            List<Category> list2 = Arrays.asList(objectObjectValueOperations.get(key).toArray(new Category[0]));
            return list2;
        } else {
            List<Category> list = categoryMapper.select(category_name, pageUtil);
            // Category类型转换为object类型 -- 万物皆对象
            List<Object> objList = Arrays.asList(list.toArray(new Category[0]));
            // 写入缓存
            objectObjectValueOperations.set(key, objList);
            return list;
        }


    }

    @Override
    public Category get(Integer category_id) {
        return categoryMapper.selectOne(category_id);
    }

    /**
     *
     * @param category_name
     * @return 分类的总条数
     */
    @Override
    public Integer getTotal(String category_name) {
        return categoryMapper.selectTotal(category_name);
    }
}
